/**
 * theme：Jquery-file-upload控件模拟easy插件写的统一接口
 *author：杨波
 * date：2018-11-12
 * versions：1.0.0
 */
(function ($) {
    // var allowTypes = []
    // var failTypes = []
    var formmatterData = {
            id: 'fileId',
            name: 'fileName',
            thumb: 'thumbUrl',
            download: 'htmlUrl'
        },
        formmatterUpload = {
            id: 'mid',
            name: 'fileName',
            thumb: 'thumbUrl',
            download: 'htmlUrl'
        },
        control = {
            //多个文件上传
            multiple: function (container, options) {
                if (options.height != 'auto') {
                    container.style.overflow = "auto";
                }
                container.className += " fileUpload-multiple";
                var upload = document.createElement("div");
                upload.className = "fileUpload-item fileUpload-btn";
                upload.title = "点击上传";
                container.appendChild(upload);
                var fileInput = document.createElement("input");
                fileInput.type = "file";
                fileInput.multiple = true;
                fileInput.name = 'files[]';
                upload.appendChild(fileInput);

            },
            //类似文本框的上传样式
            input: function (container, options) {
                container.className += " fileUpload-input";
                var text = document.createElement("input");
                text.type = "text";
                text.className = "fileUpload-text";
                text.readOnly = true;
                container.appendChild(text);

                var btnToolbar = document.createElement("div");
                btnToolbar.className = "fileUpload-item fileUpload-btn";
                container.appendChild(btnToolbar);

                var upload = document.createElement("div");
                upload.className = "fileUpload-btn-item fileUpload-btn-upload";
                upload.innerHTML = "上传按钮";
                btnToolbar.appendChild(upload);

                var fileInput = document.createElement("input");
                fileInput.type = "file";
                fileInput.multiple = false;
                fileInput.name = 'files[]';
                upload.appendChild(fileInput);

                var del = document.createElement("div");
                del.className = "fileUpload-btn-item fileUpload-btn-del";
                del.innerHTML = "删除";
                btnToolbar.appendChild(del);

                if (options.height != 'auto') {
                    text.style.lineheight = options.height;
                    btnToolbar.style.top = (options.height - 34) / 2;
                }
                control.addEvent(upload, "click", function () {
                    $(fileInput).click();
                });

            },
            //头像类型的上传样式
            image: function (container) {
                container.className += " fileUpload-img";
                var upload = document.createElement("div");
                upload.className = "fileUpload-item fileUpload-btn";
                upload.title = "点击上传";
                container.appendChild(upload);
                var fileInput = document.createElement("input");
                fileInput.type = "file";
                fileInput.multiple = false;
                fileInput.name = 'files[]';
                upload.appendChild(fileInput);

                // this.addEvent(upload,"click",function () {
                //     $(fileInput).click();
                // });
            },
            //拖拽类型的上传样式
            drag: function (container, options) {
                alert("暂未开发！");
            },
            //支持拍照上传的单个文件上传
            photographic: function (container, options) {
                container.className += " fileUpload-photographic";
                var upload = document.createElement("div");
                upload.className = "fileUpload-item fileUpload-btn";
                // upload.title = "点击上传";
                container.appendChild(upload);

                var icon = document.createElement("div");
                icon.className = "fileUpload-item-icon";
                upload.appendChild(icon);

                var i = document.createElement("i");
                i.className = 'fa fa-camera';
                icon.appendChild(i);

                var img = document.createElement("img");
                img.className = "show-img";
                upload.appendChild(img);

                var mask = document.createElement("div");
                mask.className = "fileUpload-item-mask";
                upload.appendChild(mask);

                var toolbar = document.createElement("div");
                toolbar.className = "fileUpload-item-toolbar";
                upload.appendChild(toolbar);

                var toolbarChild = document.createElement("div");
                toolbar.appendChild(toolbarChild);

                var del = document.createElement("div");
                del.className = "fileUpload-item-del";
                del.title = "删除";
                toolbarChild.appendChild(del);

                var fileInput = document.createElement("input");
                fileInput.type = "file";
                fileInput.multiple = false;
                fileInput.name = 'files[]';
                upload.appendChild(fileInput);

                var loading = document.createElement("div");
                loading.className = "fileUpload-item-loading";
                upload.appendChild(loading);
                var img_loading = document.createElement("img");
                img_loading.src = '/static/global/img/loading-img.gif';
                img_loading.className = "loading-img";
                loading.appendChild(img_loading);

                control.addEvent(del, "click", function () {
                    control.remove(options, options.data.length - 1);
                });
            },
            //创建控件
            createControl: function (options) {
                var fileUpload = document.createElement("div");
                fileUpload.className = "fileUpload-panel";
                if (options.width != 'auto') {
                    fileUpload.style.width = options.width;
                }
                if (options.height != 'auto') {
                    fileUpload.style.height = options.height;
                }
                if (options.multiple) {
                    options.singleType = 'multiple';
                }
                options.multiple = true;
                try {
                    if (this[options.singleType]) {
                        this[options.singleType](fileUpload, options);
                    } else {
                        throw "设置单选样式类型错误";
                    }
                } catch (err) {
                    console.error("错误信息：" + "设置单选样式类型错误");
                }
                return fileUpload;
            },
            //绑定事件
            addEvent: function (obj, type, handle) {
                try { // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
                    obj.addEventListener(type, handle, false);
                } catch (e) {
                    try { // IE8.0及其以下版本
                        obj.attachEvent('on' + type, handle);
                    } catch (e) { // 早期浏览器
                        obj['on' + type] = handle;
                    }
                }
            },
            //生成上传控件
            setFileupload: function (options, jq) {
                var fileOptions = {
                    url: options.url,
                    dropZone: options.drag ? jq : undefined,
                    autoUpload: false,
                    multipart: options.multiple,
                    dataType: 'json',
                    add: function (e, data) {
                        if (e.isDefaultPrevented()) {
                            return false;
                        }
                        if (options.acceptFileTypes) {
                            var fileTypes = '',
                                acceptFileTypes = undefined;
                            if (typeof options.acceptFileTypes == 'string') {
                                var types = options.acceptFileTypes.split(",");
                                for (var i = 0; i < types.length; i++) {
                                    if (fileTypes) {
                                        fileTypes += "|";
                                    }
                                    fileTypes += types[i].toLowerCase();
                                }
                                if (fileTypes) {
                                    fileTypes = "/(\\.|\\/)(" + fileTypes + ")$/i";
                                    acceptFileTypes = eval('(' + fileTypes + ')');
                                }
                            } else {
                                alert("上传文件类型格式不正确！");
                                return false;
                            }
                            var name = data.files[0]["name"].toLowerCase();
                            if (!acceptFileTypes.test(name)) {
                                alert("上传文件类型不正确！");
                                return false;
                            }
                        }
                        if (options.onStart && null != options.onStart) {
                            try {
                                var result = options.onStart(data);
                                if (result == false) {
                                    return;
                                }
                            } catch (msg) {
                                console.log(msg);
                                return;
                            }
                        }

                        control.addItem(options, data);
                        data.submit();

                        // // 判断文件是否允许上传
                        // const presentFileNameArr = data.files[0].name.split('.')
                        // const presentFileType = presentFileNameArr[presentFileNameArr.length - 1]
                        //
                        // if (allowTypes.indexOf(presentFileType) >= 0) {
                        //     control.addItem(options, data);
                        //     data.submit();
                        // } else {
                        //     if (failTypes.indexOf(presentFileType) < 0) {
                        //         failTypes.push(presentFileType)
                        //     }
                        // }
                        //
                        // const presentFileName = data.files[0].name
                        //
                        // const lastFileName = data.originalFiles[data.originalFiles.length - 1].name
                        //
                        // if (presentFileName === lastFileName && failTypes.length > 0) {
                        //     alert(`上传${failTypes.join(',')}文件类型格式不正确！`);
                        //     failTypes = []
                        // }
                    },
                    done: function (e, data) {
                        console.log(e, data,e.isDefaultPrevented())
                        if (e.isDefaultPrevented()) {
                            return false;
                        }
                        var boo = false,
                            resultData = null;
                        if (data.result) {
                            if (data.result.success) {
                                var item = data.result.obj[0];
                                item.index = data.index;
                                item.type = 'new';
                                resultData = item;
                                boo = true;
                                control.completeDom(data,item)
                                if (options.singleType == "multiple") {
                                    options.data[data.index].data = item;
                                    options.data[data.index].process = 100;
                                } else {
                                    clearInterval(options.data[data.index].interval);
                                    control.addItem(options, item);
                                }
                            } else {
                                control.failUploadFn(data.index, jq)
                                clearInterval(options.data[data.index].interval);
                                alert('不支持该类型文件上传');
                                control.remove(options, data.index);
                            }
                        } else {
                            control.failUploadFn(data.index, jq)
                            clearInterval(options.data[data.index].interval);
                            control.remove(options, data.index);
                            alert('没有返回结果');
                        }
                        if (options.onDone && null != options.onDone) {
                            try {
                                options.onDone(boo, resultData);
                            } catch (msg) {
                                console.error(msg);
                            }
                        }
                    },
                    progress: function (e, data) {
                        // console.log(e, data, 'progress')
                        // if (e.isDefaultPrevented()) {
                        //     return false;
                        // }
                        // var process = parseInt(data.loaded / data.total * 100, 10);
                        // if (process > 80) process = 80;
                        // options.data[data.index].process = process;
                        // options.data[data.index].file = data;
                        // if (options.onProgress && null != options.onProgress) {
                        //     try {
                        //         options.onProgress(options.data[data.index].process, data);
                        //     } catch (msg) {
                        //         console.log(msg);
                        //     }
                        // }
                    },
                    fail: function (e, data) {
                        // console.log(e, data, data, 'fail')
                        if (e.isDefaultPrevented()) {
                            return false;
                        }
                        clearInterval(options.data[data.index].interval);
                        alert('上传失败');
                        control.failUploadFn(data.index, jq)
                        // if (options.onFail && null != options.onFail) {
                        // 	try {
                        // 		options.onFail(data);
                        // 	} catch (msg) {
                        // 		console.log(msg);
                        // 	}
                        // }
                        // control.remove(options, data.index);
                    }
                };
                if (options.params != null && options.params) {
                    fileOptions.formData = options.params;
                }
                if (options.headers != null && options.headers) {
                    fileOptions.headers = options.headers;
                }
                jq.fileupload(fileOptions);

            },
            // 图片上传失败
            failUploadFn: function (index, jq) {
                var clip = jq.find(".item-" + index + ' .fileUpload-item-progress-clip')
                clip.css('background', 'url()')
                clip.css('background-size', '100%')
                clip.css('border', 'initial')
                clip.css('border-radius', 'initial')
                clip.css('clip', 'none')
            },
            //添加新项
            addItem: function (options, data) {
                if (!data.index && 0 != data.index) {
                    data.index = 0;
                    if (options.singleType == 'multiple' && options.data) {
                        data.index = options.data.length;
                    } else if (options.singleType == 'photographic' && options.data) {
                        data.index = 0;
                        options.data = [];
                        options.control.find("img.show-img").attr({
                            title: "",
                            src: ""
                        });
                    }
                    if (!options.data) {
                        options.data = [];
                    }
                    options.data.push({
                        type: 'add',
                        index: data.index
                    });
                    createItem[options.singleType](options, options.data[data.index]);
                    createItem.interval(options, data);
                } else {
                    options.data[data.index] = data;
                    createItem[options.singleType](options, options.data[data.index]);

                    var ids = [];
                    for (var i = 0; i < options.data.length; i++) {
                        if ("del" != options.data[i].type && 'add' != options.data[i].type) {
                            if ("new" == options.data[i].type) {
                                ids.push(options.data[i][formmatterUpload.id]);
                            } else {
                                ids.push(options.data[i][formmatterData.id]);
                            }
                        }
                    }
                    options.control.parent().find(".fileUpload").val(ids.join(","));
                    options.control.parent().find(".fileUpload").change();
                }
            },
            //删除
            remove: function (options, index, boo) {
                var item;
                if (options.singleType == 'multiple') {
                    item = options.data[index];
                    if (options.onDelete && !boo) {
                        options.onDelete(item, function (boo) {
                            if (boo === false) return;
                            control.removeNode(options, index);
                        });
                        return
                    }
                }
                control.removeNode(options, index);
            },
            //删除前端节点
            removeNode: function (options, index) {
                if (options.singleType == 'multiple') {
                    options.control.find(".item-" + index).remove();
                    options.data[index].type = "del";
                } else if (options.singleType == 'image') {
                    options.control.find(".item-" + index).remove();
                    options.control.find(".fileUpload-btn").show();
                    options.data = [];
                } else if (options.singleType == 'photographic') {
                    options.data = [];
                    options.control.find("img.show-img").attr({
                        title: "",
                        src: ""
                    }).css("display", "none");
                    options.control.find(".fileUpload-item").removeClass("hasImg");
                    options.control.find(".fileUpload-item").removeClass("loading");
                }
                var ids = [];
                for (var i = 0; i < options.data.length; i++) {
                    if ("del" != options.data[i].type && 'add' != options.data[i].type) {
                        if ("new" == options.data[i].type) {
                            ids.push(options.data[i][formmatterUpload.id]);
                        } else {
                            ids.push(options.data[i][formmatterData.id]);
                        }
                    }
                }
                options.control.parent().find(".fileUpload").val(ids.join(","));
                options.control.parent().find(".fileUpload").change();
            },
            //初始化值
            setData: function (options, data) {
                if (options.data && options.data.length > 0) {
                    for (var i = 0; i < options.data.length; i++) {
                        control.remove(options, i, true);
                    }
                }
                options.data = [];
                if (data && data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        data[i].type = 'old';
                        data[i].index = i;
                        options.data.push(data[i]);
                        control.addItem(options, data[i]);
                    }
                }
            },
            //图片添加完成后处理
            completeDom: function (data,item){
                var showType = ['png', 'jpg', 'jpeg', 'gif', 'jfif']
                var clip = jq.find(".item-" + data.index + ' .fileUpload-item-progress-clip')
                setTimeout(()=>{
                    if (showType.indexOf(item.fileType) >= 0) {
                        clip.css('background', `url(${item.htmlUrl})`)
                    } else {
                        clip.css('background', `url("/static/global/img/file/${item.fileType}.png")`)
                    }
                    clip.css('background-size', '100%')
                    clip.css('border', 'initial')
                    clip.css('border-radius', 'initial')
                    clip.css('clip', 'none')
                },4000)
            }
        },
        createItem = {
            //多个文件上传
            multiple: function (options, data) {
                if (data.type == 'add') {
                    createItem.common_progress_create(options, data);
                } else {
                    createItem.common_item_create(options, data);
                }
            },
            //类似文本框的上传样式
            input: function (options, data) {
                var proessDiv = document.createElement("div");
                proessDiv.className = "fileUpload-item-mask";

                options.control.before($(proessDiv));
            },
            //头像类型的上传样式
            image: function (options, data) {
                options.control.find(".fileUpload-btn").hide();
                if (data.index === 0) {
                    createItem.multiple(options, data);
                }
            },
            //拖拽类型的上传样式
            drag: function (options, data) {
                alert("暂未开发！");
            },
            //支持拍照上传的单个文件上传
            photographic: function (options, data) {
                if (data.index === 0) {
                    var item = options.control.find(".fileUpload-item");
                    item.addClass("hasImg");
                    if (data.type == 'add') {
                        var width = item.width() < item.height() ? item.width() : item.height();
                        options.control.find("img.loading-img").css({
                            width: width / 2 + 'px',
                            'margin-top': -width / 4 + 'px',
                            'margin-left': -width / 4 + 'px',
                        });
                        item.addClass("loading");
                    } else {
                        options.control.find("img.show-img").attr({
                            title: data[formmatterUpload.name],
                            src: data[formmatterUpload.thumb]
                        }).css("display", "block");
                        options.control.find(".fileUpload-item").removeClass("loading");
                    }
                }
            },
            //image模式下进度条和删除下载功能按钮样式调整
            image_progress: function (options, toolEle, ele) {
                if (options.singleType == 'image') {
                    var width = options.control.width(),
                        height = options.control.height();
                    if (ele && width >= 100 && height >= 100) {
                        ele.style.left = (width - 100) / 2 + "px";
                        ele.style.top = (height - 100) / 2 + "px";
                    }
                    toolEle.style.marginTop = (height - 30) / 2 + "px";
                }
            },
            //多个文件上传和头像类型的上传 上传中样式
            common_progress_create: function (options, data) {
                var width = options.control.width(),
                    firstRow = (data.index + 1) <= parseInt(width / 150) ? " first-row" : "",
                    rowLastItem = (data.index + 1) == parseInt(width / 150) ? " row-last-item" : "";
                var item = document.createElement("div");
                item.className = "fileUpload-item item-" + data.index + firstRow + rowLastItem;
                if (this.isIE8()) {
                    var progress = document.createElement("div");
                    progress.className = 'fileUpload-item-progress fileUpload-item-img';
                    item.appendChild(progress);
                } else {
                    var progress = document.createElement("div");
                    progress.className = 'fileUpload-item-progress';
                    item.appendChild(progress);

                    var clip = document.createElement("div");
                    clip.className = 'fileUpload-item-progress-clip';
                    progress.appendChild(clip);

                    // var left = document.createElement("div");
                    // left.className = 'fileUpload-item-progress-clip-left';
                    // clip.appendChild(left);
                    // var right = document.createElement("div");
                    // right.className = 'fileUpload-item-progress-clip-right width-none';
                    // clip.appendChild(right);

                    // var num = document.createElement("div");
                    // num.className = 'fileUpload-item-progress-num';
                    // num.innerHTML = '0%';
                    // progress.appendChild(num);

                    var mask = document.createElement("div");
                    mask.className = "fileUpload-item-mask";
                    item.appendChild(mask);

                    var toolbar = document.createElement("div");
                    toolbar.className = "fileUpload-item-toolbar";
                    item.appendChild(toolbar);

                    var toolbarChild = document.createElement("div");
                    toolbar.appendChild(toolbarChild);

                    var del = document.createElement("div");
                    del.className = "fileUpload-item-del";
                    del.title = "删除";
                    toolbarChild.appendChild(del);

                    control.addEvent(del, "click", function () {
                        control.remove(options, data.index);
                    });

                    createItem.image_progress(options, toolbarChild, progress);
                }
                options.control.find(".fileUpload-btn").before($(item));
            },
            //多个文件上传和头像类型的上传 上传完后样式
            common_item_create: function (options, data) {
                var item = options.control.find(".item-" + data.index)[0];
                if (item) {
                    item.innerHTML = '';
                } else {
                    var width = options.control.width();
                    var firstRow = (data.index + 1) <= parseInt(width / 150) ? " first-row" : "";
                    var rowLastItem = (data.index + 1) == parseInt(width / 150) ? " row-last-item" : "";
                    item = document.createElement("div");
                    item.className = "fileUpload-item item-" + data.index + firstRow + rowLastItem;
                    options.control.find(".fileUpload-btn").before($(item));
                }
                item.title = data[formmatterUpload.name];
                var img = document.createElement("img");
                img.src = data[formmatterUpload.thumb];
                img.className = "fileUpload-item-thumb";
                item.appendChild(img);

                if (options.showName) {
                    item.className += " showName";
                    var name = document.createElement("div");
                    name.className = "fileUpload-item-name";
                    name.innerHTML = data.fileName;
                    item.appendChild(name);
                }

                var mask = document.createElement("div");
                mask.className = "fileUpload-item-mask";
                item.appendChild(mask);

                var toolbar = document.createElement("div");
                toolbar.className = "fileUpload-item-toolbar";
                item.appendChild(toolbar);

                var toolbarChild = document.createElement("div");
                toolbar.appendChild(toolbarChild);

                var download = document.createElement("div");
                download.className = "fileUpload-item-download";
                download.title = "下载";
                toolbarChild.appendChild(download);

                var del = document.createElement("div");
                del.className = "fileUpload-item-del";
                del.title = "删除";
                toolbarChild.appendChild(del);

                control.addEvent(download, "click", function () {
                    if (!!window.ActiveXObject || 'ActiveXObject' in window) {
                        window.open(url, '_blank')
                    } else {
                        var a = document.createElement("a"), //创建a标签
                            e = document.createEvent("MouseEvents"); //创建鼠标事件对象
                        e.initEvent("click", false, false); //初始化事件对象
                        a.href = options.data[data.index][formmatterUpload.download]; //设置下载地址
                        a.download = options.data[data.index][formmatterUpload.name]; // 设置下载文件名
                        a.dispatchEvent(e); //给指定的元素，执行事件click事件
                    }
                });

                control.addEvent(del, "click", function () {
                    control.remove(options, data.index);
                });
                createItem.image_progress(options, toolbarChild, null);
            },
            //判断是否是ie8及以下
            isIE8: function () {
                var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
                var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
                if (isIE) {
                    var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
                    reIE.test(userAgent);
                    var fIEVersion = parseFloat(RegExp["$1"]);
                    if (fIEVersion <= 8) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            },
            interval: function (options, data) {
                var jq = options.control;

                var clip = jq.find(".item-" + data.index + ' .fileUpload-item-progress-clip')
                // left = jq.find(".item-" + data.index + ' .fileUpload-item-progress-clip-left'),
                // right = jq.find(".item-" + data.index + ' .fileUpload-item-progress-clip-right'),
                // num = jq.find(".item-" + data.index + ' .fileUpload-item-progress-num'),
                clip.css('background', 'url()')
                clip.css('background-size', '100%')
                clip.css('clip', 'none')
                rotate = 0,
                    loop = setInterval(function () {
                        if (options.singleType == "multiple" && options.data[data.index].process && rotate < options.data[data.index].process) {
                            if (rotate >= 100) {
                                rotate = 0;
                                // right.addClass('width-none');
                                clip.removeClass('auto');
                            } else if (rotate > 50) {
                                // right.removeClass('width-none');
                                clip.addClass('auto');
                            }
                            rotate++;
                            // left.css("transform", 'rotate(' + 3.6 * rotate + 'deg)');
                            // num.html(rotate + '%');
                            if (rotate == 100) {
                                clearInterval(loop);
                                control.addItem(options, options.data[data.index].data);
                            }
                        }
                    }, 30);
                options.data[data.index].process = 0;
                options.data[data.index].interval = loop;
            }
        };

    function init(container) {
        var state = $.data(container, 'fileUpload');
        var cc = $(container);
        cc.addClass('fileUpload');
        cc.attr("type", "hidden");
        state.options.control = $(control.createControl(state.options));
        cc.before(state.options.control);
        control.setFileupload(state.options, state.options.control);
        var data = state.options.data;
        state.options.data = [];
        control.setData(state.options, data);
        if (state.options.readOnly) {
            if (state.options.singleType != "photographic") {
                state.options.control.find(".fileUpload-btn").hide();
            } else {
                state.options.control.find(".fileUpload-item-icon").hide();
                state.options.control.find("input").hide();
            }
            state.options.control.find(".fileUpload-item-del").hide();
        }
        createToolTip(state.options);
    }

    function createToolTip(options) {
        if (options.required && !options.isCreateTip) {
            var required = $.fn.validatebox.defaults.rules.required;
            options.control.parent().find(".fileUpload").change(function () {
                if (!$(this).val() && !options.isCreateTip) {
                    options.isCreateTip = true;
                    options.control.tooltip({
                        position: options.tipPosition,
                        content: required.message,
                    })
                } else {
                    options.isCreateTip = false;
                    options.control.tooltip("destroy");
                }
            })
        }
    }

    $(function () {
        $("input.fileUpload").each(function () {
            var t = $(this);
            var options = {};
            var s = $.trim(t.attr('data-options'));
            if (s) {
                if (s.substring(0, 1) != '{') {
                    s = '{' + s + '}';
                }
                options = (new Function('return ' + s))();
            }
            $(this).fileUpload(options);
        });
    })
    $.fn.fileUpload = function (options, param) {
        if (typeof options == 'string') {
            return $.fn.fileUpload.methods[options](this, param);
        }
        options = options || {};
        return this.each(function () {
            var state = $.data(this, 'fileUpload');
            if (state) {
                $.extend(state.options, options);
            } else {
                $.data(this, 'fileUpload', {
                    options: $.extend({}, $.fn.fileUpload.defaults, options),
                    fileUpload: $(this).addClass('fileUpload')
                });
                init(this);
            }
        });
    };

    $.fn.fileUpload.methods = {
        getData: function (jq) {
            var data = $.data(jq[0], "fileUpload").options.data,
                result = [];
            if (data && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    if ("del" != data[i].type && 'add' != data[i].type) {
                        result.push(data[i]);
                    }
                }
            }
            return result;
        },
        setData: function (jq, data) {
            var state = $.data(jq[0], "fileUpload");
            control.setData(state.options, data);
        },
        setUrl: function (jq, url) {
            var state = $.data(jq[0], "fileUpload");
            state.options.url = url;
            state.options.control.fileupload('option', 'url', url);
        },
        isReadOnly: function (jq, boo) {
            var state = $.data(jq[0], "fileUpload");
            if (boo) {
                state.options.control.find(".fileUpload-btn").hide();
                state.options.control.find(".fileUpload-item-del").hide();
            } else {
                state.options.control.find(".fileUpload-btn").show();
                state.options.control.find(".fileUpload-item-del").show();
            }
        },
        isRequired: function (jq, boo) {
            var state = $.data(jq[0], "fileUpload");
            state.options.required = boo;
            if (boo) {
                if (!state.options.isCreateTip) {
                    createToolTip(state.options);
                }
                state.options.isCreateTip = true;
            } else {
                if (state.options.isCreateTip) {
                    state.options.control.tooltip("destroy");
                }
                state.options.isCreateTip = false;
            }
        },
        hasUpload: function (jq) {
            var data = $.data(jq[0], "fileUpload").options.data,
                result = [];
            if (data && data.length > 0) {
                for (var i = 0; i < data.length; i++) {
                    if ("add" == data[i].type) {
                        return true;
                    }
                }
            }
            return false;
        }
    };

    $.fn.fileUpload.parseOptions = function (target) {
        var t = $(target);
        return $.extend({}, $.parser.parseOptions(target, [
            'width', 'height',
            {
                multiple: 'boolean',
                url: 'string',
                data: 'Array',
                disabled: 'boolean',
                autoUpload: 'boolean',
                acceptFileTypes: 'string',
                onStart: 'function',
                onProgressall: 'function',
                onDone: 'function',
                onFail: 'function',
            }
        ]));
    };

    $.fn.fileUpload.defaults = {
        width: 'auto',
        height: 'auto',
        singleType: 'input',
        drag: false,
        required: false,
        isCreateTip: false,
        tipPosition: "right",
        isPhotograph: false, //是否拍摄
        prompt: '请上传文件', //在输入框显示提示消息。
        multiple: false, //是否单个文件上传 false为单个 true为多个 默认false
        delUrl: undefined, //同步删除
        // url: "/filestore/item/save", //异步上传
        url: "/filestore/item/upload", //异步上传
        data: [], //文件初始显示值
        params: null, //添加参数
        readOnly: false, //是否禁用
        acceptFileTypes: undefined, //上传文件类型
        control: undefined, //未封装的上传插件
        id: 'fileId',
        uploadId: 'mid',
        showName: false,
        headers: null, //提交后台抬头
        onStart: null, //上传开始前的执行逻辑
        onProgress: null, //上传进度的回调函数
        onDone: null, //上传完成后的执行逻辑
        onFail: null, //上传失败后的执行逻辑
        onVilidate: null, //验证上传文件是否正确合理
        onDelete: null //删除文件前
    };
})(jQuery);
